﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class BankAccount : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadAccount();
            Label4.Text = "";
        }
    }
    private void loadAccount()
    {
        if (Session["Username"] != null)
        {
            String username = Session["UserName"].ToString();
            ChoDienTuDataContext cdt = new ChoDienTuDataContext();
            var user = cdt.Select_CustomerByUsername(username).First();

            int? bankId = user.Bank_Account_ID;

            lblAccountNumer.Text = bankId.ToString();

            Bank2DataContext bank = new Bank2DataContext();
            var acc = bank.Select_AccountById(bankId).First();
            lblBalance.Text = acc.Balance.ToString();
        }
        else
        {
            lblAccountNumer.Text = "";
            lblBalance.Text = "";
        }
    }
    protected void btnDrawIn_Click(object sender, EventArgs e)
    {
        String l1 = lblAccountNumer.Text;
        String l2 = lblBalance.Text;

        if (l1 != "" && l2 != "" && txtSum.Text != "")
        {
            int id = int.Parse(l1);
            float balance = float.Parse(l2);
            float sum = float.Parse(txtSum.Text);

            Bank2DataContext bank = new Bank2DataContext();
            bank.DrawIn(id, sum);
            bank.SubmitChanges();
            Label4.Text = "Nạp tiền thành công";
        }
        loadAccount();
    }
    protected void btnDrawOut_Click(object sender, EventArgs e)
    {
        String l1 = lblAccountNumer.Text;
        String l2 = lblBalance.Text;

        if (l1 != "" && l2 != "" && txtSum.Text != "")
        {
            int id = int.Parse(l1);
            float balance = float.Parse(l2);
            float sum = float.Parse(txtSum.Text);

            Bank2DataContext bank = new Bank2DataContext();
            bank.DrawOut(id, sum);
            bank.SubmitChanges();
            Label4.Text = "Rút tiền thành công";
        }
        loadAccount();
    }
}